# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=heimdal
pkgname=('heimdal' 'heimdal-libs' 'heimdal-devel')
pkgver=7.5.0
pkgrel=3
pkgdesc="Implementation of Kerberos V5 libraries"
arch=('i686' 'x86_64')
url="https://www.h5l.org/"
license=('custom')
makedepends=('libcrypt-devel' 'libedit-devel' 'libsqlite-devel' 'openssl-devel') #libldap-devel
options=('!libtool' '!emptydirs' 'staticlibs')
groups=('net-utils')
source=(https://github.com/heimdal/heimdal/releases/download/heimdal-7.5.0/heimdal-${pkgver}.tar.gz{,.sig}
        1.2.1-test-modules.patch
        1.5.2-hdbdir.patch
        1.5.2-install-catman.patch
        1.5.2-roken-signal.patch
        1.5.3-missing-libs-pkg-config.patch
        0001-Work-around-LN_S-ln.patch
        7.5.0-names-clash-with-openssl.patch
        0001-do-link-zlib-together-with-libcrypto.patch)
sha256sums=('c5a2a0030fcc728022fa2332bad85569084d1c3b9a59587b7ebe141b0532acad'
            'SKIP'
            'b18c9ca3f2db08cea0a4b7d7f07e8656360af848b3a30a5fa8d748240989da49'
            'eb4b2a170a65225789b554948108d6a073e31b4118a2461c1375b046ba360b2d'
            'd68bb0f8f01cc748a6ef63c74a4a15c75553c2322e9bd2649c54cb23077d521b'
            'bf19c5d7326a0cf58a5b4922a9c96fb78807226bc40c2248417024b961dd559e'
            'e4b122e880638a0e1f62feca1c7261d816335386c467155f202e3f1f4282a200'
            '2217945f181942872f1ccdef1b1e3a15d2f757ec1270621dcacdebbf539b4384'
            'bfe321c0391d6f7e23abecc22e9941bcb79d0b67abb9975246a9f462ec32513b'
            '1960c9cf2f10c83881fc1cb0fd8a03576a18f29cceebae6e7fb09f2871430fdd')
validpgpkeys=('E65941B71CF3C459A34FA89C45E7572A28CD8CC8') # Heimdal Release Key

prepare() {
  cd ${srcdir}/heimdal-${pkgver}

  patch -p2 -i "${srcdir}"/1.2.1-test-modules.patch
  patch -p1 -i "${srcdir}"/1.5.2-hdbdir.patch
  patch -p2 -i "${srcdir}"/1.5.2-install-catman.patch
  patch -p2 -i "${srcdir}"/1.5.2-roken-signal.patch
  patch -p1 -i "${srcdir}"/1.5.3-missing-libs-pkg-config.patch
  patch -p1 -i "${srcdir}"/0001-Work-around-LN_S-ln.patch
  patch -p1 -i "${srcdir}"/7.5.0-names-clash-with-openssl.patch
  patch -p1 -i "${srcdir}"/0001-do-link-zlib-together-with-libcrypto.patch

  autoreconf -fi
}

build() {
  cd ${srcdir}/heimdal-${pkgver}

  ./configure --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/libexec \
    --sbindir=/usr/bin \
    --sysconfdir=/etc/krb5 \
    --mandir=/usr/share/man \
    --datadir=/var/lib/heimdal \
    --localstatedir=/var/lib/heimdal \
    --with-openssl=/usr \
    --with-libedit=/usr \
    --with-sqlite3=/usr \
    --without-berkeley-db \
    --disable-otp \
    --enable-shared=yes \
    --enable-static \
    --without-x \
    --disable-kcm \
    --disable-ndbm-db \
    --disable-heimdal-documentation
  
  counter=0
  while ! make LDFLAGS="${LDFLAGS} -no-undefined"
  do
    counter=$(($counter+1))
    test $counter -lt 5 || {
      echo "Could not build (tried 5 times)" >&2
      exit 1
    }
  done

  setup
}

setup() {
  local destdir=${srcdir}/dest
  cd ${srcdir}/heimdal-${pkgver}
  make DESTDIR=${destdir} install

  # Remove server (and its daemon) and their manpages
  for i in kdc kadmind; do
    rm ${destdir}/usr/share/man/man8/${i}.8
    rm ${destdir}/usr/libexec/${i}
  done

  # Rename clients and their manpages
  for i in bsearch su; do
    if [ -f ${destdir}/usr/share/man/man1/${i}.1 ]; then
      mv ${destdir}/usr/share/man/man1/{,k}${i}.1
    fi
    mv ${destdir}/usr/bin/{,k}${i}
  done
  rm -rf ${destdir}/usr/share/man/cat{1,3,5,8}

  # install config
  # Do not install the config as this will conflict with krb5
  install -D -m644 ${srcdir}/heimdal-${pkgver}/krb5.conf ${destdir}/etc/krb5/krb5.conf

  # Remove conflicts
  rm -rf ${destdir}/usr/share/man/man5/ftpusers.5*                     # man-pages
  rm -rf ${destdir}/usr/share/man/man3/{DES,DH,EVP,OpenSSL,RAND,RSA}*  # openssl
  rm -rf ${destdir}/usr/share/man/man3/os.3*                           # erlang

  # Install the license
  install -Dm644 ${srcdir}/heimdal-${pkgver}/LICENSE \
    ${destdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_heimdal() {
  groups=('sys-utils')
  depends=('heimdal-libs')
  backup=(etc/krb5/krb5.conf)

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  rm -f ${pkgdir}/usr/bin/*.dll
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_heimdal-libs() {
  depends=('libcrypt' 'libedit' 'libsqlite' 'libopenssl')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_heimdal-devel() {
  pkgdesc="Heimdal headers and libraries"
  groups=('development')
  depends=('heimdal-libs' 'libcrypt-devel' 'libedit-devel' 'libsqlite-devel')

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
}
